package pro.softzhang.algo.lc100;

import pro.softzhang.algo.common.TreeNode;
import pro.softzhang.algo.flag.difficulty.DRed;

/**
 *
 */
@DRed
public class LC129_SumRootToLeafNumbers {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, new TreeNode(2, null, null), new TreeNode(3, null, null));
        int sum = new Solution().sumNumbers(root);
        System.out.println(sum);
    }

    static class Solution {
        public int sumNumbers(TreeNode root) {
            return doSumNumbers(root, 0);
        }

        private int doSumNumbers(TreeNode root, int pathSum) {
            if(root == null) {
                return 0;
            }

            pathSum = pathSum * 10 + root.val;
            if(root.left == null && root.right == null) {
                return pathSum;
            }
            return doSumNumbers(root.left, pathSum) + doSumNumbers(root.right, pathSum);
        }
    }
}